package ciyuanwutuobang;

import ciyuanwutuobang.Core.Command;
import ciyuanwutuobang.Core.Config;
import ciyuanwutuobang.Core.KickCountManager;
import ciyuanwutuobang.Core.PlayerManager;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 150149
 */
public class MainClass extends JavaPlugin implements Listener {
    public static long lasttime;
    private PlayerManager playerManager;
    public KickCountManager kickCountManager;
    private Writer logWriter;
    public static MainClass self;
    public Config config;
    public static String testPlayer = "";

    @Override
    public void onEnable(){
        playerManager = new PlayerManager();
        kickCountManager = new KickCountManager();
        config = new Config(this.getConfig(),this);
        self = this;
        this.getCommand("cace").setExecutor(new Command(this));
        Bukkit.getPluginManager().registerEvents(new AntiCE(playerManager),this);
        Bukkit.getPluginManager().registerEvents(this, this);
        Bukkit.getConsoleSender().sendMessage("[CiYuanAntiCE]反变速已开启");
    }

    @Override
    public void onDisable() {
        Bukkit.getConsoleSender().sendMessage("[CiYuanAntiCE]反变速已关闭");
    }

    public void sendToOP(String msg) {
        Player[] l = Bukkit.getServer().getOnlinePlayers().toArray(new Player[0]);
        for (Player pl : l ){
            if (pl.isOp()) {
                pl.sendMessage(msg);
            }
        }
    }

    public static void handleViolection(Player p) {
        BukkitTask time= new BukkitRunnable() {
            @Override
            public void run() {
                p.kickPlayer(MainClass.self.config.kick_message);
                this.cancel();
            }
        }.runTaskTimer(self, 40L,0L);
    }

    public static void handleBanPlayer(Player p) {
        BukkitTask time= new BukkitRunnable() {
            @Override
            public void run() {
                p.kickPlayer(MainClass.self.config.ban_message);
                Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(),"ban " + p.getName() + " " + MainClass.self.config.ban_message);
                this.cancel();
            }
        }.runTaskTimer(self, 40L,0L);
    }

    public void logInfo(String log) {
        this.getLogger().info(log);
        try {
            if (logWriter == null) {
                logWriter = new FileWriter(new File(this.getDataFolder(), "CiYuanAntiCE.log"), true);
            }
            logWriter.write(String.format("[%s]%s\n", new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date()), log));
            logWriter.flush();
            logWriter.close();
            logWriter=null;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void broadcast(Player player) {
        Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(),"bc " + player.getName() + "已经多次超速，请立刻关闭任何非法软件！");
    }

    public static void debug(String msg) {
        //Bukkit.getConsoleSender().sendMessage(msg);
    }

}
